package com.example.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {

    // 引入 redis 模板(redis 本身)
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 在 Redis 中存储数据
     */
    @RequestMapping("set")
    public String setRedis() {
        stringRedisTemplate.opsForValue().set("username", "lisi", 100);
        return "Redis 存储成功";
    }

    /**
     *  从 redis 中读取数据
     */
    @RequestMapping("/get")
    public String getRedis() {

        return stringRedisTemplate.opsForValue().get("username");
    }

}
